package com.example.dbmysql.Controller;


        import com.example.dbmysql.pojo.Buildings;
        import com.example.dbmysql.pojo.RoomType;
        import com.example.dbmysql.result.PageResult;
        import com.example.dbmysql.result.Result;
        import com.example.dbmysql.service.RoomTypeService;
        import com.github.pagehelper.Page;
        import com.github.pagehelper.PageHelper;
        import com.github.pagehelper.PageInfo;
        import org.apache.ibatis.annotations.Update;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.validation.annotation.Validated;
        import org.springframework.web.bind.annotation.*;
        import java.util.List;

@RestController
@RequestMapping("/roomType")
@Validated
public class roomTypeController {
    
    @Autowired
    private RoomTypeService roomTypeService;

    @PostMapping("/add")
    public Result add(@RequestBody RoomType roomType) {
        RoomType b = roomTypeService.findByTypeName(roomType.getTypeName());
        if (b == null) {
            roomTypeService.add(roomType);
            return Result.success();
        } else {
            return Result.error("房型已存在");
        }
    }

    @GetMapping("/all")
    public PageResult all(@RequestParam int page, @RequestParam int pageSize) {


        PageHelper.startPage(page, pageSize);
        // 查询全部数据
        PageInfo<RoomType> pageInfo = roomTypeService.selectAll(page, pageSize);
        // 获取总记录数据
        long total = pageInfo.getTotal();

        List<RoomType> NowList = pageInfo.getList();

        return PageResult.success(NowList, total, page, pageSize);

    }

    @PutMapping("/update")
    public  Result update(@RequestBody @Validated RoomType roomType){

        RoomType UpdateRoom =   roomTypeService.updateById(roomType);

        System.out.println(UpdateRoom);

        if(UpdateRoom == null) return Result.error("更新失败");
        return Result.success();
    }

    @GetMapping("/geRoomType/{id}")
    public Result getBuildings(@PathVariable("id")  Integer id){

        RoomType b =  roomTypeService.geRoomTypeById(id);

        if(b == null) return Result.error("查询失败");

        return Result.success(b);
    }

    @DeleteMapping("/delbuildings/{id}")
    public Result delRoomType(@PathVariable("id") @Validated  Integer id){

        RoomType b =  roomTypeService.geRoomTypeById(id);

        if(b == null) return Result.error("房型不存在");

        roomTypeService.delRoomTypeById(id);

        return Result.success();

    }
}

